package com.blast.comicsisland.adapters;

/**
 * Created by ibm on 2015/6/26.
 */

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.blast.comicsisland.R;
import com.blast.comicsisland.utils.RequestManager;

import java.util.List;
import java.util.Map;

/**
 * 用于专题界面的具体内容显示的适配器，
 * 也就是TopicContentActivity中ListView的Adapter
 */
public class TopicContentAdapter extends BaseAdapter{
    private Context context;
    private List<Map<String, Object>> list;
    private ImageLoader imageLoader;
    public TopicContentAdapter(Context context, List<Map<String, Object>> list){
        this.context = context;
        this.list = list;

        imageLoader = RequestManager.getInstance().getImageLoader();
    }

    @Override
    public int getCount() {
        int ret = 0;
        if (list != null){
            ret = list.size();
        }
        return ret;
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public int getItemViewType(int position) {
        int ret = 0;
        String type = (String )list.get(position).get("description");
        if (type.equals("")){
            ret = 0;
        }else {
            ret = 1;
        }
        return ret;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View ret = null;
        //String type = (String )list.get(position).get("description");
        int type = getItemViewType(position);
        if (type == 0) {
            if (convertView == null) {
                ret = LayoutInflater.from(context).inflate(R.layout.item_topic_content2, parent, false);
            } else {
                ret = convertView;
            }
            ViewHolder2 viewHolder2 = (ViewHolder2) ret.getTag();
            if (viewHolder2 == null) {
                viewHolder2 = new ViewHolder2();
                viewHolder2.networkImageView = (NetworkImageView) ret.findViewById(R.id.topic_content_img2);
                ret.setTag(viewHolder2);
            }
            String url = (String) list.get(position).get("coverurl");

            if (url != null && !url.equals("")) {

                viewHolder2.networkImageView.setDefaultImageResId(R.drawable.imgbg);
                viewHolder2.networkImageView.setErrorImageResId(R.drawable.ic_launcher);
                viewHolder2.networkImageView.setImageUrl(url, imageLoader);
            }


        } else  if (type == 1){
            if (convertView == null) {
                ret = LayoutInflater.from(context).inflate(R.layout.item_topic_cotent, parent, false);
            } else {
                ret = convertView;
            }
            ViewHolder viewHolder = (ViewHolder) ret.getTag();
            if (viewHolder == null) {
                viewHolder = new ViewHolder();
                viewHolder.networkImageView = (NetworkImageView) ret.findViewById(R.id.topic_content_img);
                viewHolder.textView = (TextView) ret.findViewById(R.id.topic_content_txt);
                ret.setTag(viewHolder);
            }
            String url = (String) list.get(position).get("coverurl");
            String description = (String) list.get(position).get("description");


            if (url != null && !url.equals("")) {

                viewHolder.networkImageView.setDefaultImageResId(R.drawable.imgbg);
                viewHolder.networkImageView.setErrorImageResId(R.drawable.ic_launcher);
                viewHolder.networkImageView.setImageUrl(url, imageLoader);
            }
            if (description != null) {
                viewHolder.textView.setText(description);
            }

        }
        return ret;
    }

    private static class ViewHolder{
        private NetworkImageView networkImageView;
        private TextView textView;
    }
    private static class ViewHolder2{
        private NetworkImageView networkImageView;
    }

}
